import org.mindrot.jbcrypt.BCrypt;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class User {
    private String username;
    private String password;
    private int id; // 新增数据库ID

    // 新增getter/setter
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    private final List<Diary> diaries = new ArrayList<>();

    // 必须的无参构造器（用于反射/框架实例化）
    public User() {}

    // 注册时使用的构造器
    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    // 核心日记操作方法
    public boolean removeDiary(String diaryId) {
        return diaries.removeIf(d -> d.getId().equals(diaryId));
    }

    public Optional<Diary> getDiaryById(String id) {
        return diaries.stream()
                .filter(d -> d.getId().equals(id))
                .findFirst();
    }

    // 日记集合访问器
    public List<Diary> getDiaries() {
        return diaries; // 直接返回引用（注意线程安全）
    }

    public void addDiary(Diary diary) {
        diaries.add(diary);
    }

    // Getter/Setter
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

//    public void setPassword(String password) {
//        this.password = password;
//    }
    // 修改密码设置方法
    public void setPassword(String password) {
        this.password = BCrypt.hashpw(password, BCrypt.gensalt());
    }

    // 添加密码验证方法
    public boolean verifyPassword(String candidate) {
        return BCrypt.checkpw(candidate, this.password);
    }
}